<template>
  <div class="shop_main">
    <el-card>
      <el-form ref="formRef" :model="form" style="width: 100%">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="快递单号" prop="transfer">
              <el-input v-model="form.transfer" style="width: 100%" placeholder="请输入快递单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出库类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择出库类型" style="width: 100%">
                <el-option label="快递出库" :value="1"></el-option>
                <el-option label="自提出库" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="" prop="name">
              <el-button :icon="Search" type="primary" @click="search">查 询</el-button>
              <el-button :icon="Refresh" @click="reset">重 置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    &nbsp;
    <el-card>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="transfer" label="快递单号" min-width="130" align="center">
          <template #default="{ row }">
            {{ row.transfer || '--' }}
          </template>
        </el-table-column>
        <el-table-column prop="total" label="出库商品数量" min-width="120" align="center">
          <template #default="{ row }">
            <el-button link type="primary" @click="openShop(row)">{{ row.shopList.length }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="成本价" align="center"></el-table-column>
        <el-table-column prop="salePrice" label="销售价" min-width="120" align="center"></el-table-column>
        <el-table-column prop="transferPrice" label="快递费用" min-width="120" align="center"></el-table-column>
        <el-table-column prop="memo" label="客户信息" min-width="120" align="center">
          <template #default="{ row }">
            {{ row.memo || '--' }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="出库类型" min-width="120" align="center">
          <template #default="{ row }">
            <el-tag v-if="row.type == 1" size="small" type="success">快递出库</el-tag>
            <el-tag v-else size="small" type="info">自提出库</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170" align="center">
          <template #default="{ row }">
            {{ formattedDateTime(row.createTime) }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="action" label="操作" width="150" fixed="right" align="center">
          <template #default="{ row }">
            <el-button link type="primary" :disabled="row.type == 2" :icon="Download" @click="openEdit(row)">导出收货单</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      &nbsp;
      <el-pagination
        style="display: flex; justify-content: flex-end"
        background
        :current-page="pagenation.page"
        :page-size="pagenation.limit"
        :total="pagenation.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    &nbsp;

    <Edit v-model="showEdit" :info="currentEdit" @close="closeEdit" @reload="loadData" />
    <Shop v-model="showShop" :info="currentShop" @close="closeShop" />
  </div>
</template>

<script setup>
  import { getOutPage } from '@/server/out';
  import { ref, onMounted } from 'vue';
  import { ElMessage } from 'element-plus';
  import { Search, Refresh, Plus, EditPen, Delete, Download } from '@element-plus/icons-vue';
  import { useDateFomatter } from '@/hooks/formatter';
  import Edit from './edit.vue';
  import Shop from './shop.vue';

  const { formattedDateTime } = useDateFomatter();
  const form = ref({
    name: '',
    type: '',
  });
  const formRef = ref(null);
  const pagenation = ref({
    page: 1,
    limit: 10,
    total: 0,
  });
  const tableData = ref([]);

  const showEdit = ref(false);
  const showShop = ref(false);
  const currentEdit = ref({});
  const currentShop = ref([]);

  const handleSizeChange = size => {
    pagenation.value.limit = size;
    loadData();
  };
  const handleCurrentChange = page => {
    pagenation.value.page = page;
    loadData();
  };

  function openAdd() {
    currentEdit.value = {};
    showEdit.value = true;
  }
  function openEdit(row) {
    currentEdit.value = row;
    showEdit.value = true;
  }
  function openShop(row) {
    currentShop.value = row.shopList;
    showShop.value = true;
  }

  function closeEdit() {
    showEdit.value = false;
  }

  function closeShop() {
    showShop.value = false;
  }

  async function delCallback(id) {
    const res = await deleteType({ id });
    if (res.code == 200) {
      ElMessage.success('操作成功');
      loadData();
    }
  }

  function search() {
    loadData();
  }
  function reset() {
    formRef.value.resetFields();
    loadData();
  }

  async function loadData() {
    const searchModel = { ...form.value, ...pagenation.value };
    if (form.value.createTime) {
      searchModel.startTime = form.value.createTime[0];
      searchModel.endTime = form.value.createTime[1];
    } else {
      delete searchModel.startTime;
      delete searchModel.endTime;
    }
    const res = await getOutPage(searchModel);
    if (res.code === 200) {
      tableData.value = res.data.rows;
      pagenation.value.total = res.data.total;
    }
  }

  onMounted(() => {
    loadData();
  });
</script>

<style scoped>
  ::v-deep(.el-form-item__label) {
    font-weight: 700;
  }

  .shop_main {
    position: relative;
    width: 100%;
    height: 100%;
  }
</style>
